package delegate4j;

import com.sun.istack.internal.Nullable;

import java.util.ArrayList;
import java.util.concurrent.FutureTask;

public class Action9<T1, T2, T3, T4, T5, T6, T7, T8, T9> extends Delegate<IAction9<T1, T2, T3, T4, T5, T6, T7, T8, T9>> {

    public Action9(IAction9<T1, T2, T3, T4, T5, T6, T7, T8, T9> delegate) {
        super(delegate);
    }

    public void invoke(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9) throws Exception {
        for (IAction9<T1, T2, T3, T4, T5, T6, T7, T8, T9> action : delegates) {
            action.invoke(t1, t2, t3, t4, t5, t6, t7, t8, t9);
        }
    }

    public AsyncResult<Void> invokeAsync(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, @Nullable AsyncDelegateCallback callback) {
        ArrayList<FutureTask<Void>> tasks = new ArrayList<>();
        for (int i = 0; i < delegates.size(); i++) {
            tasks.add(delegates.get(i).invokeAsync(t1, t2, t3, t4, t5, t6, t7, t8, t9, null));
        }

        waitTasksFinish(tasks, new Object[]{t1, t2, t3, t4, t5, t6, t7, t8, t9}, callback);

        //返回tasks的浅拷贝
        return new AsyncResult<>(tasks);
    }
}